#include "StdAfx.h"

#include "FloorsManager.h"
#include "listeners.h"
#include "Passenger.h"

CFloorsManager::CFloorsManager(void)
{
}

CFloorsManager::~CFloorsManager(void)
{
	for (FloorsCollection::iterator it = m_floors.begin(); it != m_floors.end(); ++it)
		delete *it;
}

void CFloorsManager::GenerateFloors(size_t num)
{
	m_floors.clear();
	m_floors.reserve(num);
	for (size_t i=0; i<num; ++i)
		m_floors.push_back(new CFloor(i));
}

void CFloorsManager::GeneratePassengers(size_t maxPassengerPerFloor)
{
	for (FloorsCollection::iterator it = m_floors.begin(); it != m_floors.end(); ++it)
	{
		PassengerCollection inPassengers;
		CFloor* curFloor = *it;
		size_t pasNum = static_cast<size_t>(rand()) % maxPassengerPerFloor;
		inPassengers.reserve(pasNum);
		for (size_t i=0; i<pasNum; ++i)
		{
			size_t targetNum = static_cast<size_t>(rand()) % m_floors.size();
			if (targetNum == curFloor->GetNum()) // ensure that we won't generate passenger that no needs to go on other floor
			{ 
				if (targetNum == m_floors.size()-1)
					--targetNum;
				else
					++targetNum;
			}
			inPassengers.push_back(new CPassenger(targetNum));
		}

		curFloor->AddIncomingPassengers(inPassengers);
	}
}